package com.youlu.campus.service.timetable;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.youlu.campus.common.utils.DateTimeUtils;
import com.youlu.campus.entity.timetable.UserTimetableConfig;
import com.youlu.campus.entity.timetable.dto.UserTimetableConfigCreateDTO;
import com.youlu.campus.entity.timetable.dto.UserTimetableConfigQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;

/**
 * @author : [dengbo]
 * @className : UserTimetableConfigService
 * @description : [用户课程表配置信息]
 * @createTime : [2023/4/21 10:01]
 */
@Service
@Slf4j
public class UserTimetableConfigService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    @Lazy
    private UserTimetableService userTimetableService;

    public UserTimetableConfig getUserConfig(UserTimetableConfigQueryDTO dto) {
        return this.getUserConfig(dto.getActivityId(), dto.getUserId());
    }

    public UserTimetableConfig getUserConfig(String activityId, String userId) {
        String grade = "2022-2023";
        String semester = "1";
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("activityId").is(activityId));
        UserTimetableConfig config = mongoTemplate.findOne(query, UserTimetableConfig.class);
        if (config == null) {
            config = new UserTimetableConfig();
            config.setUserId(userId);
            config.setActivityId(activityId);
            config.setCourseNum(16);
            String dateStr = "2023-02-20";
//            LocalDate date = LocalDateTimeUtil.parseDate(dateStr);
            Date date= DateTimeUtils.StringToDate(dateStr);
            config.setStartDateStr(dateStr);
            config.setStartDate(date);
            config.setYear(String.valueOf(date.getYear()));
            config.setSemester(semester);
            config.setGrade(grade);
            mongoTemplate.insert(config);
        }
        return config;
    }


    public UserTimetableConfig saveConfig(UserTimetableConfigCreateDTO param) {
        String dateStr = StringUtils.isBlank(param.getStartDate()) ? "2023-02-20" : param.getStartDate();
//        LocalDate date = LocalDateTimeUtil.parseDate(dateStr);
        Date date= DateTimeUtils.StringToDate(dateStr);
        Query query = new Query();
        String grade = "2022-2023";
        String semester = "1";
        query.addCriteria(Criteria.where("userId").is(param.getUserId()).and("activityId").is(param.getActivityId()));
        UserTimetableConfig config = mongoTemplate.findOne(query, UserTimetableConfig.class);
        if (config == null) {
            config = new UserTimetableConfig();
            config.setUserId(param.getUserId());
            config.setActivityId(param.getActivityId());
            config.setCourseNum(16);
            config.setStartDateStr(dateStr);
            config.setStartDate(date);
            config.setYear(String.valueOf(date.getYear()));
            config.setSemester(semester);
            config.setGrade(grade);
            mongoTemplate.insert(config);
            return config;
        }
        boolean updateCourse = !config.getStartDateStr().equals(dateStr);
        config.setStartDateStr(dateStr);
        config.setStartDate(date);
        config.setYear(String.valueOf(date.getYear()));
        mongoTemplate.save(config);
        if (updateCourse) {
            // 更新课程表信息
            userTimetableService.updateUserAllCourseByConfig(config);
        }
        return config;
    }
}
